package com.service.pay.diamond.service;

import com.service.pay.diamond.dao.OrderDao;
import com.service.pay.diamond.dao.UnifiedOrderDao;
import com.service.pay.diamond.dto.OrderDto;
import com.service.pay.diamond.dto.request.PayOrderDto;
import com.service.pay.diamond.dto.request.UnifiedOrderDto;
import com.service.pay.diamond.dto.response.PrepareOrderDto;
import com.service.pay.diamond.event.PayOrderEvent;
import com.service.pay.diamond.mapper.OrderMapper;
import com.service.pay.diamond.mapper.UnifiedOrderMapper;
import com.service.pay.diamond.model.OrderModel;
import com.service.pay.diamond.model.UnifiedOrderModel;
import com.service.pay.diamond.tool.DmUtil;
import com.tool.base.exception.ApiException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;

@Service
public class OrderService {
    @Autowired
    private ApplicationContext context;
    @Autowired
    private OrderDao orderDao;
    @Autowired
    private UnifiedOrderDao unifiedOrderDao;
    @Autowired
    private UnifiedOrderMapper unifiedOrderMapper;
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private DmUtil dmUtil;


    /*查询订单*/
    public OrderDto queryOrder(String orderNo) {
        return orderMapper.modelToDto(orderDao.getByOrder(orderNo));
    }

    /*预下单*/
    public PrepareOrderDto unifiedOrder(UnifiedOrderDto unifiedDto) {
        // 查询是否已预下单
        UnifiedOrderModel unifiedOrderModel = unifiedOrderDao.getByOrderNo(unifiedDto.getOrderNo());
        if (null != unifiedOrderModel) {
            return unifiedOrderMapper.modelToDto(unifiedOrderModel);
        }
        UnifiedOrderModel unifiedOrderModelNew = unifiedOrderMapper.dtoToModel(unifiedDto);
        unifiedOrderModelNew.setPrepayId(dmUtil.generatePrepareId());
        unifiedOrderModelNew = unifiedOrderDao.save(unifiedOrderModelNew);
        return unifiedOrderMapper.modelToDto(unifiedOrderModelNew);
    }

    /*支付订单*/
    public OrderDto payOrder(PayOrderDto payOrderDto) {
        UnifiedOrderModel prepareOrder = unifiedOrderDao.getByPrepayId(payOrderDto.getPrepayId());
        if (null == prepareOrder) {
            throw new ApiException("预下单不存在:" + payOrderDto, 404);
        }
        OrderModel orderModel = prepareOrder.getOrder();
        if (null == orderModel) {
            orderModel = new OrderModel();
        }
        if (1 == orderModel.getStatus()) {
            throw new ApiException("订单已支付:" + orderModel, 500);
        }
        return doPayOrder(prepareOrder, orderModel);
    }

    public OrderDto doPayOrder(UnifiedOrderModel prepareOrder, OrderModel orderModel) {
        orderModel = orderMapper.unifiedModelToOrderModel(prepareOrder, orderModel);
        orderModel.setStatus(1);
        orderModel.setResult("SUCCESS");
        orderModel.setPayDateTime(new Date());
        // 更新预下单数据
        prepareOrder.setOrder(orderModel);
        unifiedOrderDao.save(prepareOrder);
        context.publishEvent(new PayOrderEvent(this, orderModel));
        return orderMapper.modelToDto(orderModel);
    }


    /*支付订单后通知回调url*/
    public void notifyOrder(OrderModel orderModel) {
        String notifyUrl = orderModel.getNotifyUrl();
        if (!StringUtils.isEmpty(notifyUrl)) {
            // TODO 异步通知订单
        }
    }
}
